<?php

namespace iBrand\Component\Point\Model;

use iBrand\Shop\Core\Models\User;
use Illuminate\Database\Eloquent\Model;
use iBrand\Component\Order\Models\Order;
use iBrand\Component\Order\Models\OrderItem;

class Point extends Model
{

	protected $table   = 'el_point';
	protected $guarded = ['id'];
	protected $appends = ['point_order_no', 'expired'];

	public function __construct(array $attributes = [])
	{
		$this->table = config('point.point_table');

		parent::__construct($attributes);

		$this->valid_time = $this->getPointValidTime();
	}

	public function user()
	{
		return $this->belongsTo(User::class, 'user_id')->withDefault();
	}

	public function point_order()
	{

		return $this->hasOne(Point::class, 'id')
			->where('item_type', 'iBrand\Component\Order\Models\Order')->with('order');
	}

	public function point_order_item()
	{

		return $this->hasOne(Point::class, 'id')
			->where('item_type', 'iBrand\Component\Order\Models\OrderItem')->with('order_item.order');
	}

	public function order()
	{

		return $this->belongsTo(Order::class, 'item_id');
	}

	public function order_item()
	{

		return $this->belongsTo(OrderItem::class, 'item_id');
	}

	public function getModelAttribute()
	{
		$model = $this->item_type;
		$model = new $model();

		return $model;
	}

	public function getOrder()
	{
		$order = false;
		$type  = $this->item_type;
		if ($type == 'iBrand\Component\Order\Models\Order') {
			$order = $this->model;
			$order = $order->find($this->item_id);
		} elseif ($type == 'iBrand\Component\Order\Models\OrderItem') {
			$orderItem = $this->model;
			$order     = $orderItem->find($this->item_id)->order;
		}

		return $order;
	}

	public function scopeType($query, $type)
	{
		return $query->where('type', $type);
	}

	public function scopeAction($query, $action)
	{
		return $query->where('action', $action);
	}

	public function scopeSumPoint($query)
	{
		return $query->sum('value');
	}

	public function scopeValid($query)
	{
		return $query->whereRaw('(DATEDIFF(now(),created_at) < valid_time or valid_time = 0)')->where('status', 1);
	}

	public function scopeOverValid($query)
	{
		return $query->whereRaw('(DATEDIFF(now(),created_at) > valid_time and valid_time <> 0)')->where('status', 1);
	}

	public function scopeWithinTime($query)
	{
		return $query->whereRaw('(DATEDIFF(now(),created_at) < valid_time or valid_time = 0)');
	}

	public function getPointOrderNoAttribute()
	{
		$order_no = '';
		$type     = $this->item_type;
		if ($type == 'iBrand\Component\Order\Models\Order') {
			return $this->order ? $this->order->order_no : '';
			/*return isset($this->point_order->order->order_no)?$this->point_order->order->order_no:'';*/
		} elseif ($type == 'iBrand\Component\Order\Models\OrderItem') {
			return $this->order_item ? $this->order_item->order->order_no : '';
			/*return isset($this->point_order_item->order_item->order->order_no)?$this->point_order_item->order_item->order->order_no:'';*/
		}

		return $order_no;
	}

	public function getPointValidTime()
	{

	    return 0;
		/*if (app()->runningInConsole()) {
			return 0;
		}
		$valid_time = settings('point_valid_time_setting') ? settings('point_valid_time_setting') : 0;

		if ($valid_time == 0) {
			return $valid_time;
		}

		return \Carbon\Carbon::now()->addDay($valid_time)->getTimestamp();*/
	}

	public function getExpiredAttribute()
	{
		if ($this->valid_time == 0) {
			return 0;
		} else {
			return (time() - strtotime($this->created_at)) / 86400 > $this->valid_time ? 1 : 0;
		}
	}

	/*
	public static function migrate($table, $array)
	{
		$results = DB::table($table)->get();
		foreach ($results as $result){
			$result = json_decode(json_encode($result),true);
			Point::create([
				'user_id' => isset($result[$array['user_id']]) ? $result[$array['user_id']] : null,
				'type' => isset($result[$array['type']]) ? $result[$array['type']] : 'default',
				'action' => isset($result[$array['action']]) ? $result[$array['action']] : null,
				'note' => isset($result[$array['note']]) ? $result[$array['note']] : null,
				'value' => isset($result[$array['value']]) ? $result[$array['value']] : null,
				'valid_time' => isset($result[$array['valid_time']]) ? $result[$array['valid_time']] : 0,
				'item_id' => isset($result[$array['item_id']]) ? $result[$array['item_id']] : null,
				'created_at' => isset($result[$array['created_at']]) ? $result[$array['created_at']] : null,
				'updated_at' => isset($result[$array['updated_at']]) ? $result[$array['updated_at']] : null,
				'deleted_at' => isset($result[$array['deleted_at']]) ? $result[$array['deleted_at']] : null,
			]);
		}
		return 'Done!';
	}
	*/

}
